package com.xxxx.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类-Direct模式
 */
@Configuration
public class RabbitMQDirectConfig {

    @Bean
    public Queue queue() {
        // true：消息持久化
        // 如果队列配置为持久化，消息也配置为持久化，就可以配置持久化
        // 如果队列不配置为持久化，消息配置为持久化，就无法持久化
        return new Queue("queue", true);
    }

    private static final String QUEUE01 = "queue_direct01";
    private static final String QUEUE02 = "queue_direct02";
    private static final String EXCHANGE = "directExchange";
    private static final String ROUTINGKEY01 = "queue.red";
    private static final String ROUTINGKEY02 = "queue.green";

    @Bean
    public Queue queue01() {
        return new Queue(QUEUE01);
    }

    @Bean
    public Queue queue02() {
        return new Queue(QUEUE02);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE);
    }

    @Bean
    public Binding bindingDirect01() {
        return BindingBuilder
                .bind(queue01())
                .to(directExchange())
                .with(ROUTINGKEY01);
    }

    @Bean
    public Binding bindingDirect02() {
        return BindingBuilder
                .bind(queue02())
                .to(directExchange())
                .with(ROUTINGKEY02);
    }
}
